#include "httpreader.h"

HttpReader::HttpReader(QString site,int port, QString request, QString file) :site(site), request(request)
{
    socket=new QTcpSocket(this);
    connect(socket,SIGNAL(connected()),this,SLOT(connected()));
    connect(socket,SIGNAL(readyRead()),this,SLOT(read()));
    connect(socket,SIGNAL(disconnected()),this,SLOT(close()));
    socket->connectToHost(site,port);
    f=new QFile(file);
    f->open(QIODevice::WriteOnly);
}

void HttpReader::connected() {

    QByteArray conn;
    conn.append("GET "+request+"\r\n");

     qDebug()<<"connected, sending request"<<"GET "+request;
    socket->write(conn);
}

void HttpReader::read() {
    qDebug()<<"reading data";
    f->write(socket->readAll());
}

void HttpReader::close() {
    f->close();
    qDebug()<<"done writing"<<f->fileName();
}
